package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.dto.UserDTO;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IUserService;
import com.example.mybatisplusdemo.model.domain.User;
import org.springframework.web.bind.support.SessionStatus;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-17
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;



    /**
     * 描述：根据Id 查询
     */
    // http://127.0.0.1:8080/api/user/1
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
//    public JsonResponse<User> getById(@PathVariable("id") Long id)throws Exception {
//        User user = userService.getById(id);
//        return JsonResponse.success(user);
    @GetMapping("getdemo1")
    public String s(Long id, String name) {
        System.out.println(id);
        System.out.println(name);
        return "hello";
    }

    @GetMapping("getdemo2")
    public String s2(UserDTO userDTO) {
        System.out.println(userDTO.getId());
        System.out.println(userDTO.getName());
        return "hello";
    }

    //占位符传参
    @GetMapping("getdemo3/{id}/{id2}")
    public String s3(@PathVariable("id") Long xx) {
        System.out.println(xx);
        return "hello";
    }

    @PostMapping("postdemo1")
    public String s4(@RequestBody User user) {
        System.out.println(user);
        return  "hello";

    }

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }

//    @GetMapping("getPage")
//    public JsonResponse<Page<User>> getUserList(
//            @RequestParam(defaultValue = "1") int pageNum,
//            @RequestParam(defaultValue = "10") int pageSize,
//            @RequestParam(required = false) String login_name,
//            @RequestParam(required = false) String password) {
//
//        // 使用 MyBatis-Plus 的 Page 对象进行分页查询
//        Page<User> result = userService.pageQuery(pageNum, pageSize, login_name, password);
//        return JsonResponse.success(result);
//    }

    @GetMapping("listPage")
    public JsonResponse listPage(PageDTO pageDTO, User user) {
        Page<User> page = userService.listPage(pageDTO, user);
        return JsonResponse.success(page);
    }


    @GetMapping("listByKey")
    public JsonResponse listByKey(String key){
        List<User> userList = userService.listByKey(key);
        return JsonResponse.success(userList);
    }

    @PostMapping("login")
    public JsonResponse login(@RequestBody User user){
         User login = userService.login(user);
         return JsonResponse.success(login);
    }

    @GetMapping("getInfo")
    public JsonResponse getInfo(){
        User user = SessionUtils.getCurrentUserInfo();
        return JsonResponse.success(user);
    }

    @GetMapping("removeById")
    public JsonResponse removeById(Long id){
        boolean b = userService.removeById(id);
        return JsonResponse.success(b);
    }

    @PostMapping("updateById")
    public JsonResponse updateById(@RequestBody User user){
        boolean b = userService.updateById(user);
        return JsonResponse.success(b);
    }


    //rest风格
    //所有请求都带有目的 增删改查
    //get delete post put

}


